package com.bst.etl.datax.web;

import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.core.page.TableDataInfo;
import com.bst.common.utils.HdpDateUtil;
import com.bst.datatx.core.biz.ExecutorBiz;
import com.bst.datatx.core.biz.model.LogResult;
import com.bst.datatx.core.biz.model.ReturnT;
import com.bst.etl.datax.core.scheduler.JobScheduler;
import com.bst.etl.datax.entity.JobGroup;
import com.bst.etl.datax.entity.JobLog;
import com.bst.etl.datax.mapper.JobGroupMapper;
import com.bst.etl.datax.mapper.JobLogMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by on 17/5/10.
 */
@Controller
@RequestMapping("/base/job/log")
public class JobLogController extends BaseController {

    private String prefix = "etl/job/log";

    @Resource
    private JobLogMapper jobLogMapper;
    @Resource
    private JobGroupMapper jobGroupMapper;

    @GetMapping()
    public String page( ModelMap mmap)
    {
        return prefix + "/log";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JobLog jobLog)
    {
        startPage();
        List<JobLog> list = jobLogMapper.pageList(jobLog);
        return getDataTable(list);
    }

    @GetMapping("/selectById")
    @ResponseBody
    public AjaxResult selectById(Integer logId)
    {
        startPage();
        JobLog log = jobLogMapper.load(logId);
        return AjaxResult.success(log);
    }

    @RequestMapping(value = "/logDetailCat", method = RequestMethod.GET)
    @ApiOperation("运行日志详情")
    @ResponseBody
    public AjaxResult logDetailCat(long logId) {
        try {
            JobLog jobLog = jobLogMapper.load(logId);
            JobGroup jobGroup = jobGroupMapper.load(jobLog.getJobGroup());
            ExecutorBiz executorBiz = JobScheduler.getExecutorBiz(jobGroup.getAddressList());
            ReturnT<LogResult> logResult = executorBiz.log(jobLog.getTriggerTime().getTime(), logId, 1);

            // is end
            if (logResult.getContent() != null && 1 > logResult.getContent().getToLineNum()) {
                if (jobLog.getHandleCode() > 0) {
                    logResult.getContent().setEnd(true);
                }
            }
            if(logResult.getCode()==ReturnT.SUCCESS_CODE) {
                return AjaxResult.success(logResult.getContent());
            } else {
                return AjaxResult.error(logResult.getMsg());
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String dateType) {
        Date now = new Date();
        Date dateEnd=null;
        if("weekAgo".equals(dateType)) {
            dateEnd = HdpDateUtil.calculate(now, Calendar.DAY_OF_MONTH,-7);
        } else if("monthAgo".equals(dateType)) {
            dateEnd = HdpDateUtil.calculate(now, Calendar.MONTH,-1);
        }
        jobLogMapper.deleteByDate(dateEnd);
        return AjaxResult.success();
    }
}
